package cn.jhc.teachingquality.net.teaching;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;

import org.jsoup.nodes.Document;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.jhc.teachingquality.Global;
import cn.jhc.teachingquality.net.TeachingQualityRequest;

import static cn.jhc.teachingquality.Global.*;
/**
 * 审核通过教学日志。
 * Created by luyanfei on 16-4-7.
 */
public class CheckTeachingLogRequest extends TeachingQualityRequest<String> {
    private static final String CHECK_TEACHING_LOG_URL =
            URL_PREFIX + "/jcKcxxAction!saveForJsSh.shtml";

    private String courseId;
    private List<String> studentIds;

    public CheckTeachingLogRequest(String courseId, List<String> studentIds) {
        super(Method.POST, CHECK_TEACHING_LOG_URL, null);
        this.courseId = courseId;
        this.studentIds = studentIds;
    }

    @Override
    protected String handleResponse(Document document) {
        return null;
    }

// getParams无法在同一key下放置多个值。
//    @Override
//    protected Map<String, String> getParams() throws AuthFailureError {
//        Map<String, String> map = new HashMap<>();
//        map.put("id", courseId);
//        map.put("jcKcxx.id", courseId);
//        for(String id : studentIds){
//            map.put("skxsIDs", id);
//        }
//        return map;
//    }

    @Override
    public byte[] getBody() throws AuthFailureError {
        StringBuilder encodedParams = new StringBuilder();
        encodedParams.append("id=").append(courseId).append("&jcKcxx.id=")
                .append(courseId).append("&");
        String paramsEncoding = getParamsEncoding();
        try {
            for (String id : studentIds) {
                encodedParams.append("skxsIDs=");
                encodedParams.append(URLEncoder.encode(id, paramsEncoding));
                encodedParams.append('&');
            }
            return encodedParams.toString().getBytes(paramsEncoding);
        } catch (UnsupportedEncodingException uee) {
            throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);
        }
    }
}
